﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PlaceOnMap.ascx.cs"
    Inherits="GNSWebSite.Controls.PlaceOnMap" %>
<h2>
    <asp:Localize ID="locShowOnMap" runat="server" meta:resourcekey="locShowOnMap"></asp:Localize>
</h2>
<div>
    <input type="text" id="txtAddress" />
    <asp:Button ID="btnPlaceAddress" OnClientClick="return false;" runat="server" meta:resourcekey="btnPlaceAddress"/>
    <asp:HiddenField ID="hfLatitude" runat="server" />
    <asp:HiddenField ID="hfLontitude" runat="server" />
</div>
<div>
    <div id="map_canvas" style="height: 400px; width: 400px;">
    </div>
</div>

<script type="text/javascript" language="javascript">

    var geocoder;
    var map;
    var m;
    var map_marker;
    var zoom_for_accuracy = [17, 12, 8, 6, 4, 3, 1, 0, 0];

    $(document).ready(function() {
    $("#<%= btnPlaceAddress.ClientID %>").click(function() {
            queryCity($("#txtAddress").val());
        });

        if (GBrowserIsCompatible()) {

            var address = 'new yourk';

            var baseLink = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php";
            var multimapBaseLink = "http://www.multimap.com/map/browse.cgi?scale=10000&icon=x";
            geocoder = new GClientGeocoder();

            queryCity(address);
        }
    });

    function savepoints() {

        var pointToSave = map_marker.getPoint();
        var cLat = document.getElementById('<%= hfLatitude.ClientID %>');
        var cLng = document.getElementById('<%= hfLontitude.ClientID %>');
        //   $("textarea#ExampleMessage").attr("value", result.exampleMessage);

        // $('<%= hfLatitude.ClientID %>').attr('value', pointToSave.x);
        // $('<%= hfLontitude.ClientID %>').attr('value', pointToSave.y);

        cLat.setAttribute("value", pointToSave.y);
        cLng.setAttribute("value", pointToSave.x);

        //$("#mapContainer").css("display", "none");
        return false;
    };

    function parseCoordinate(c) {
        var matches = c.match(/^(?:(?:-?[0-9]+(?:\.[0-9]+)?|\.[0-9]+)|-?[0-9]+\s+(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)|-?[0-9]+\s+[0-9]+\s+(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+))$/);
        if (!matches || matches.length != 1)
            return false;

        c = c.split(/ +/);
        c[1] = c[1] || 0; c[2] = c[2] || 0.0;
        return c.length > 0 ? (parseFloat(c[0]) + (c[1] / 60.0 + c[2] / 3600.0) * (c[0].charAt(0) == '-' ? -1 : 1)) : false;
    }

    function parseCoordinates(st) {
        st = $.trim(st);
        var c = $.map(st.split(st.indexOf(",") > 0 ? /\s*,\s*/ : /\s+/), parseCoordinate);
        if (c.length == 2 && c[0] !== false && c[1] !== false)
            return c;

        return false;
    }

    function centerMapAtLatLong(name, pt, zoom) {

        m = $("#map_canvas").get(0);
        //    $.extend(m.style, { position: "relative", left: "0px" });
        zoom = 17 - zoom;

        if (!map) {
            map = new GMap2(m);
            map.setCenter(pt, zoom, G_NORMAL_MAP);
            //map.setUIToDefault();
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.enableScrollWheelZoom();

            map_marker = new GMarker(pt, { draggable: true });
            map.addOverlay(map_marker);
            map_marker.enableDragging();
        }
        else {
            map.checkResize();
            map.setCenter(pt, zoom, G_NORMAL_MAP);
            map_marker.setPoint(pt);
        }

        GEvent.addListener(map, "click", saveMarkerPosition);
    }

    function preload() { for (var i = 0; i < arguments.length; i++) { var img = new Image(); img.src = arguments[i] } }

    function saveMarkerPosition(overlay, point) {
        if (!overlay) {
            map_marker.setPoint(point)
            savepoints();
        }
    }

    function queryCity(f) {
        var city_st = f;
        var coords = parseCoordinates(city_st);
        if (coords !== false) {
            centerMapAtLatLong(city_st, new GLatLng(coords[0], coords[1]), 1);
            return;
        }
        geocoder.getLocations(city_st, function(response) {
            if (!response || response.Status.code != 200 || response.Placemark.length == 0) {
                $("#city_results").html('We can\'t find <em>%s<\/em>.'.replace("%s", city_st.replace("<", "&lt;").replace(">", "&gt;")));
                //.showSlow();
                // $("#qc_close").click(function() { $(this).parent().hide('normal'); return false; });
            }
            else {
                if (response.Placemark.length == 1) {
                    var coor = response.Placemark[0].Point.coordinates;
                    var acc = response.Placemark[0].AddressDetails.Accuracy;
                    var z = zoom_for_accuracy[Math.max(Math.min(acc, 8), 0)];

                    centerMapAtLatLong(city_st, new GLatLng(coor[1], coor[0]), z);
                }
                else {
                    var result_html = "<ul>";

                    for (var i = 0; i < response.Placemark.length; i++) {
                        var p = response.Placemark[i].Point;
                        var a = response.Placemark[i].address;
                        var acc = response.Placemark[i].AddressDetails.Accuracy;
                        var z = zoom_for_accuracy[Math.max(Math.min(acc, 8), 0)];

                        result_html += "<li><a lt='" + p.coordinates[1] + "' ln='" + p.coordinates[0] + "' zoom='" + z + "' href='#'>" +
						a.replace("<", "&lt;").replace(">", "&gt;") +
						"</a></li>";
                    }

                    result_html += "</ul>";
                    $("#city_results").html(result_html);
                    $("#city_results ul li a").click(function() {
                        centerMapAtLatLong(city_st, new GLatLng($(this).attr('lt'), $(this).attr('ln')), parseInt($(this).attr('zoom')));
                        return false;
                    });
                    //  $("#qc_close").click(function() { $(this).parent().hide('normal'); return false; });
                }
            }
        });
    }
   
</script>

